import 'dart:io' as io;

import 'package:device_info_plus/device_info_plus.dart';
import 'package:locale_plus/locale_plus.dart';
import 'package:mobile_app.core/core/infrastructure/repositories/secret_config_repository/repository.dart';
import 'package:mobile_app.core/logger/log_level.dart';
import 'package:package_info_plus/package_info_plus.dart';

part 'application_store.dart';

part 'environment.dart';

part 'payment_mechanism.dart';

part 'platform.dart';

const _kTelegramChannelWelcomeMessageId = 10;
const _kTelegramChannelMistakeReportMessageId = 11;
const _kApiVersion = 'v1';

class Config {
  factory Config() {
    return _instance;
  }

  Config._internal();

  static final _instance = Config._internal();

  final androidAppId = 'com.meowofficial.words';
  final iosAppId = '1507234956';
  final developerEmail = 'support@angrywords.ru';
  final mistakeReportEmailSubject = 'Mistake report';
  final nativeLanguageCode = 'ru';

  final termsOfUseUrl = 'https://angrywords.ru/terms';
  final privacyPolicyUrl = 'https://angrywords.ru/privacy';

  final websitePurchasePageUrl = 'https://angrywords.ru/premium';

  final telegramChannelWelcomeMessageBrowserUrl =
      'https://t.me/angrywordsapp/$_kTelegramChannelWelcomeMessageId';

  final telegramChannelMistakeReportMessageBrowserUrl =
      'https://t.me/angrywordsapp/$_kTelegramChannelMistakeReportMessageId';

  String get telegramChannelMistakeReportMessageSchemaUrl {
    switch (platform) {
      case Platform.ios:
        return 'tg://resolve?domain=angrywordsapp&post=$_kTelegramChannelMistakeReportMessageId';
      case Platform.android:
        return 'intent://resolve?domain=angrywordsapp/$_kTelegramChannelMistakeReportMessageId'
            '#Intent;package=org.telegram.messenger;scheme=tg;end';
    }
  }

  String get telegramChannelWelcomeMessageSchemaUrl {
    switch (platform) {
      case Platform.ios:
        return 'tg://resolve?domain=angrywordsapp&post=$_kTelegramChannelWelcomeMessageId';
      case Platform.android:
        return 'intent://resolve?domain=angrywordsapp/$_kTelegramChannelWelcomeMessageId'
            '#Intent;package=org.telegram.messenger;scheme=tg;end';
    }
  }

  bool get isRussianDevice {
    return deviceCountryCode == 'RU';
  }

  PaymentMechanism get paymentMechanism {
    // if (applicationStore.isRuStore) {
    //   return PaymentMechanism.web;
    // }

    return PaymentMechanism.googleAppleStore;
  }

  late final String amplitudeApiKey;
  late final String yookassaMobileSdkKey;
  late final String yookassaShopId;

  late final LogLevel logLevel;
  late final Environment environment;
  late final Platform platform;
  late final ApplicationStore applicationStore;

  late final String serverBaseUrl;
  late final String serverApiBaseUrl;
  late final String appVersion;
  late final int appVersionCode;
  late final String model;
  late final String systemVersion;
  late final bool isPhysicalDevice;
  late final String deviceCountryCode;

  late final bool analyticsCollectionEnabled;
  late final bool crashReportsEnabled;
  late final bool purchasesEnabled;
  late final bool productionUIEnabled;
  late final bool appIntroEnabled;
  late final bool appReviewRequestsEnabled;
  late final bool learningRepetitionIntervalProductionModeEnabled;
  late final bool learningLevelProductionModeEnabled;

  Future<void> initDev({
    required ApplicationStore applicationStore,
  }) async {
    logLevel = LogLevel.verbose;
    environment = Environment.dev;

    this.applicationStore = applicationStore;

    serverBaseUrl = 'https://stage.angrywords.ru';
    serverApiBaseUrl = '$serverBaseUrl/api/$_kApiVersion';

    yookassaShopId = '925997';

    analyticsCollectionEnabled = false;
    crashReportsEnabled = false;
    purchasesEnabled = false;
    productionUIEnabled = false;
    appIntroEnabled = false;
    appReviewRequestsEnabled = false;
    learningRepetitionIntervalProductionModeEnabled = false;
    learningLevelProductionModeEnabled = true;

    _initPlatform();

    await Future.wait([
      _initSecrets(),
      _initDeviceInfo(
        platform: platform,
      ),
      _initDeviceRegion(),
      _initAppInfo(),
    ]);
  }

  /////////////////////////////////////////////////////////////////////////////////////////////////
  // PRODUCTION CONFIG
  /////////////////////////////////////////////////////////////////////////////////////////////////
  // <editor-fold defaultstate="collapsed">

  Future<void> initProd({
    required ApplicationStore applicationStore,
  }) async {
    logLevel = LogLevel.info;
    environment = Environment.prod;

    this.applicationStore = applicationStore;

    serverBaseUrl = 'https://angrywords.ru';
    serverApiBaseUrl = '$serverBaseUrl/api/$_kApiVersion';

    analyticsCollectionEnabled = true;
    crashReportsEnabled = true;
    purchasesEnabled = true;
    productionUIEnabled = true;
    appIntroEnabled = true;
    appReviewRequestsEnabled = true;
    learningRepetitionIntervalProductionModeEnabled = true;
    learningLevelProductionModeEnabled = true;

    yookassaShopId = '968657';

    _initPlatform();

    await Future.wait([
      _initSecrets(),
      _initDeviceInfo(
        platform: platform,
      ),
      _initDeviceRegion(),
      _initAppInfo(),
    ]);
  }

  // </editor-fold>

  void _initPlatform() {
    if (io.Platform.isIOS) {
      platform = Platform.ios;
    } else if (io.Platform.isAndroid) {
      platform = Platform.android;
    }
  }

  Future<void> _initDeviceInfo({
    required Platform platform,
  }) async {
    final deviceInfoPlugin = DeviceInfoPlugin();

    switch (platform) {
      case Platform.ios:
        final deviceInfo = await deviceInfoPlugin.iosInfo;
        final appleModel = _getAppleModelById(deviceInfo.utsname.machine);
        model = appleModel ?? deviceInfo.utsname.machine;
        isPhysicalDevice = deviceInfo.isPhysicalDevice;
        systemVersion = deviceInfo.systemVersion;

      case Platform.android:
        final deviceInfo = await deviceInfoPlugin.androidInfo;
        model = deviceInfo.model;
        isPhysicalDevice = deviceInfo.isPhysicalDevice;
        systemVersion = '${deviceInfo.version.sdkInt}';
    }
  }

  Future<void> _initDeviceRegion() async {
    deviceCountryCode = await LocalePlus().getRegionCode() ?? 'RU';
  }

  Future<void> _initAppInfo() async {
    final packageInfo = await PackageInfo.fromPlatform();

    appVersion = packageInfo.version;
    appVersionCode = int.parse(packageInfo.buildNumber);
  }

  Future<void> _initSecrets() async {
    final secretConfig = await const SecretConfigRepository().getSecretConfig();

    amplitudeApiKey = secretConfig.amplitudeApiKey;
    yookassaMobileSdkKey = secretConfig.yookassaMobileSdkKey;
  }
}

String? _getAppleModelById(String deviceId) {
  return switch (deviceId) {
    'iPhone1,1' => 'iPhone',
    'iPhone1,2' => 'iPhone 3G',
    'iPhone2,1' => 'iPhone 3GS',
    'iPhone3,1' || 'iPhone3,2' || 'iPhone3,3' => 'iPhone 4',
    'iPhone4,1' || 'iPhone4,2' || 'iPhone4,3' => 'iPhone 4S',
    'iPhone5,1' || 'iPhone5,2' => 'iPhone 5',
    'iPhone5,3' || 'iPhone5,4' => 'iPhone 5C',
    'iPhone6,1' || 'iPhone6,2' => 'iPhone 5S',
    'iPhone7,2' => 'iPhone 6',
    'iPhone7,1' => 'iPhone 6 Plus',
    'iPhone8,1' => 'iPhone 6S',
    'iPhone8,2' => 'iPhone 6S Plus',
    'iPhone8,4' => 'iPhone SE',
    'iPhone9,1' || 'iPhone9,3' => 'iPhone 7',
    'iPhone9,2' || 'iPhone9,4' => 'iPhone 7 Plus',
    'iPhone10,1' || 'iPhone10,4' => 'iPhone 8',
    'iPhone10,2' || 'iPhone10,5' => 'iPhone 8 Plus',
    'iPhone10,3' || 'iPhone10,6' => 'iPhone X',
    'iPhone11,8' => 'iPhone XR',
    'iPhone11,2' => 'iPhone XS',
    'iPhone11,4' || 'iPhone11,6' => 'iPhone XS Max',
    'iPhone12,1' => 'iPhone 11',
    'iPhone12,3' => 'iPhone 11 Pro',
    'iPhone12,5' => 'iPhone 11 Pro Max',
    'iPhone12,8' => 'iPhone SE 2',
    'iPhone13,1' => 'iPhone 12 mini',
    'iPhone13,2' => 'iPhone 12',
    'iPhone13,3' => 'iPhone 12 Pro',
    'iPhone13,4' => 'iPhone 12 Pro Max',
    'iPhone14,2' => 'iPhone 13 Pro',
    'iPhone14,3' => 'iPhone 13 Pro Max',
    'iPhone14,4' => 'iPhone 13 mini',
    'iPhone14,5' => 'iPhone 13',
    'iPhone14,6' => 'iPhone SE 3',
    'iPhone14,7' => 'iPhone 14',
    'iPhone14,8' => 'iPhone 14 Plus',
    'iPhone15,2' => 'iPhone 14 Pro',
    'iPhone15,3' => 'iPhone 14 Pro Max',
    'iPod1,1' => 'iPod touch 1',
    'iPod2,1' => 'iPod touch 2',
    'iPod3,1' => 'iPod touch 3',
    'iPod4,1' => 'iPod touch 4',
    'iPod5,1' => 'iPod touch 5',
    'iPod7,1' => 'iPod touch 6',
    'iPod9,1' => 'iPod touch 7',
    'iPad1,1' => 'iPad',
    'iPad2,1' || 'iPad2,2' || 'iPad2,3' || 'iPad2,4' => 'iPad 2',
    'iPad3,1' || 'iPad3,2' || 'iPad3,3' => 'iPad 3',
    'iPad3,4' || 'iPad3,5' || 'iPad3,6' => 'iPad 4',
    'iPad6,11' || 'iPad6,12' => 'iPad 5',
    'iPad7,5' || 'iPad7,6' => 'iPad 6',
    'iPad7,11' || 'iPad7,12' => 'iPad 7',
    'iPad11,6' || 'iPad11,7' => 'iPad 8',
    'iPad12,1' || 'iPad12,2' => 'iPad 9',
    'iPad4,1' || 'iPad4,2' || 'iPad4,3' => 'iPad Air',
    'iPad5,3' || 'iPad5,4' => 'iPad Air 2',
    'iPad11,3' || 'iPad11,4' => 'iPad Air 3',
    'iPad13,1' || 'iPad13,2' => 'iPad Air 4',
    'iPad13,16' || 'iPad13,17' => 'iPad Air 5',
    'iPad2,5' || 'iPad2,6' || 'iPad2,7' => 'iPad mini',
    'iPad4,4' || 'iPad4,5' || 'iPad4,6' => 'iPad mini 2',
    'iPad4,7' || 'iPad4,8' || 'iPad4,9' => 'iPad mini 3',
    'iPad5,1' || 'iPad5,2' => 'iPad mini 4',
    'iPad11,1' || 'iPad11,2' => 'iPad mini 5',
    'iPad14,1' || 'iPad14,2' => 'iPad mini 6',
    'iPad6,3' || 'iPad6,4' => 'iPad Pro 9.7-inch',
    'iPad7,3' || 'iPad7,4' => 'iPad Pro 10.5-inch',
    'iPad8,1' || 'iPad8,2' || 'iPad8,3' || 'iPad8,4' => 'iPad Pro 11-inch',
    'iPad8,9' || 'iPad8,10' => 'iPad Pro 11-inch 2',
    'iPad13,4' || 'iPad13,5' || 'iPad13,6' || 'iPad13,7' => 'iPad Pro 11-inch 3',
    'iPad6,7' || 'iPad6,8' => 'iPad Pro 12.9-inch',
    'iPad7,1' || 'iPad7,2' => 'iPad Pro 12.9-inch 2',
    'iPad8,5' || 'iPad8,6' || 'iPad8,7' || 'iPad8,8' => 'iPad Pro 12.9-inch 3',
    'iPad8,11' || 'iPad8,12' => 'iPad Pro 12.9-inch 4',
    'iPad13,8' || 'iPad13,9' || 'iPad13,10' || 'iPad13,11' => 'iPad Pro 12.9-inch 5',
    _ => null,
  };
}
